﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LantixLBSServer.Web.DbAccess.Helper;
using LantinxShare.LBS;
using System.Data;
using LantinxShare.Common;
using LantixLBSServer.Web.Common.Helper;

namespace LantixLBSServer.Web.DbAccess
{
    public class TaskManager : IManager
    {

        public int GetTaskId(String userId, String markId, String startDate, String endDate)
        {

            int result = -1;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {

                db.AddParameter("@UserId");
                db.AddParameter("@MarkId");
                db.AddParameter("@StartDate");
                db.AddParameter("@EndDate");
                db.AddParameter("@TaskType");
                db.AddParameter("@Reason");

                db.SetParameterValue("@UserId", userId);
                db.SetParameterValue("@MarkId", markId);
                db.SetParameterValue("@StartDate", startDate);
                db.SetParameterValue("@EndDate", endDate);
                db.SetParameterValue("@TaskType", GTaskType.Default);
                db.SetParameterValue("@Reason", -1);
                IDataReader reader = db.ExecuteDataReader(@"select id from t_task where UserId=@UserId and MarkId=@MarkId and StartDate=@StartDate and EndDate=@EndDate 
                and TaskType=@TaskType and Reason=@Reason ", false);
                while (reader.Read())
                {
                    result = reader.GetInt32(0);
                    break;
                }
            }, () => { db.CloseConnection(); });
            return result;
        }

        public List<GTask> GetTaskByDate(String startDate, String endDate)
        {
            List<GTask> gtaskList = new List<GTask>();
            
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@StartDate");
                db.AddParameter("@EndDate");

                db.SetParameterValue("@StartDate", startDate);
                db.SetParameterValue("@EndDate", endDate);
                IDataReader reader = db.ExecuteDataReader(@"select t1.id,t1.UserId,t1.MarkId,t1.StartDate,t1.EndDate,t1.FinishedDate,t1.TaskType,t1.Reason,
                    t2.UserName,t2.UserAlias,t2.Phone,
                    t3.MarkName,t3.Address,t3.Description,
                    t4.State,t4.workitemtypeid,
                    t5.workitemname
                    from t_task t1,t_user t2,t_mark t3,t_workitem t4,t_workitem_type t5 
                    where t1.UserId=t2.id and t1.MarkId=t3.id and t1.id=t4.taskid and t4.workitemtypeid = t5.id and (StartDate>=@StartDate and EndDate<=@EndDate)", false);
                IDictionary<int, GTask> dict = new Dictionary<int, GTask>();
                while (reader.Read())
                {
                    GTask gtask;
                    if (dict.ContainsKey(reader.GetInt32(0)))
                    {
                        gtask = dict[reader.GetInt32(0)];
                        if (gtask.WorkItem != null)
                        {
                            gtask.WorkItem.Add(new GWorkItem()
                            {
                                Id = reader.GetInt32(15),
                                State = reader.GetInt32(14),
                                WorkItemType = new GWorkItemType()
                                {
                                    WorkItemName = reader.GetString(16),
                                }
                            });
                        }
                    }
                    else
                    {
                        gtask = new GTask()
                     {
                         Id = reader.GetInt32(0),
                         User = new GUser()
                         {
                             Id = reader.GetInt32(1),
                             UserName = reader.GetString(8),
                             UserAlias = reader.GetString(9),
                             Phone = reader.GetString(10)
                         },
                         Mark = new GMark()
                         {
                             Id = reader.GetInt32(2),
                             MarkName = reader.GetString(11),
                             Address = reader.GetString(12),
                             Description = reader.GetString(13)
                         },
                         StartDate = CDateStampEx.ConvertToDateTime(reader.GetInt64(3)),
                         EndDate = CDateStampEx.ConvertToDateTime(reader.GetInt64(4)),
                         FinishedDate = CDateStampEx.ConvertToDateTime(reader.GetInt64(5)),
                         TaskType = GTaskType.Default,
                         Reason = reader.GetInt32(7),
                         WorkItem = new List<GWorkItem>()
                         {
                             new GWorkItem(){
                                 Id = reader.GetInt32(15),
                                 State = reader.GetInt32(14),
                                 WorkItemType = new GWorkItemType(){
                                     WorkItemName=reader.GetString(16),
                                 }
                             }
                         }
                     };
                        dict[gtask.Id] = gtask;
                    }
                }
                foreach (var item in dict)
                {
                    gtaskList.Add(item.Value);
                }

               
            }, () => { db.CloseConnection(); });
            return gtaskList;
        }

        public bool InserTask(GTask task)
        {
            bool result = false;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@UserId");
                db.AddParameter("@MarkId");
                db.AddParameter("@StartDate");
                db.AddParameter("@EndDate");
                db.AddParameter("@FinishedDate");
                db.AddParameter("@TaskType");
                db.AddParameter("@Reason");

                db.SetParameterValue("@UserId", task.User.Id);
                db.SetParameterValue("@MarkId", task.Mark.Id);
                db.SetParameterValue("@StartDate", task.StartDate.ToString(G.DD));
                db.SetParameterValue("@EndDate", task.EndDate.ToString(G.DD));
                db.SetParameterValue("@FinishedDate", task.FinishedDate.ToString(G.DD));
                db.SetParameterValue("@TaskType", task.TaskType);
                db.SetParameterValue("@Reason", task.Reason);

                if (db.ExecuteNonQuery(@"insert into t_task(UserId,MarkId,StartDate,EndDate,FinishedDate,TaskType,Reason) 
                        values(@UserId,@MarkId,@StartDate,@EndDate,@FinishedDate,@TaskType,@Reason)", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }


        public bool InserTask(String userId, String markId, String startDate, String endDate)
        {
            bool result = false;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@UserId");
                db.AddParameter("@MarkId");
                db.AddParameter("@StartDate");
                db.AddParameter("@EndDate");
                db.AddParameter("@FinishedDate");
                db.AddParameter("@TaskType");
                db.AddParameter("@Reason");

                db.SetParameterValue("@UserId", userId);
                db.SetParameterValue("@MarkId", markId);
                db.SetParameterValue("@StartDate", startDate);
                db.SetParameterValue("@EndDate", endDate);
                db.SetParameterValue("@FinishedDate", DateTime.MinValue.ToString(G.DD));
                db.SetParameterValue("@TaskType", GTaskType.Default);
                db.SetParameterValue("@Reason", -1);

                if (db.ExecuteNonQuery(@"insert into t_task(UserId,MarkId,StartDate,EndDate,FinishedDate,TaskType,Reason) 
                        values(@UserId,@MarkId,@StartDate,@EndDate,@FinishedDate,@TaskType,@Reason)", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }


        public bool UpdateTask(GTask task)
        {
            bool result = false;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@UserId");
                db.AddParameter("@MarkId");
                db.AddParameter("@StartDate");
                db.AddParameter("@EndDate");
                db.AddParameter("@FinishedDate");
                db.AddParameter("@TaskType");
                db.AddParameter("@Reason");
                db.AddParameter("@id");

                db.SetParameterValue("@UserId", task.User.Id);
                db.SetParameterValue("@MarkId", task.Mark.Id);
                db.SetParameterValue("@StartDate", task.StartDate.ToString(G.DD));
                db.SetParameterValue("@EndDate", task.EndDate.ToString(G.DD));
                db.SetParameterValue("@FinishedDate", task.FinishedDate.ToString(G.DD));
                db.SetParameterValue("@TaskType", task.TaskType);
                db.SetParameterValue("@Reason", task.Reason);
                db.SetParameterValue("@id", task.Id);

                if (db.ExecuteNonQuery(@"update t_task set UserId=@UserId,MarkId=@MarkId,StartDate=@StartDate,EndDate=@EndDate,
                FinishedDate@FinishedDate,TaskType=@TaskType,Reason=@Reason whrere id = @id", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool DeleteGWorkItem(int id)
        {
            bool result = false;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@id");
                db.SetParameterValue("@id", id);

                if (db.ExecuteNonQuery(@"delete from t_task where id=@id ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }
    }
}